package com.smart.redis.example.dao.impl;

import com.smart.redis.example.dao.RedisDao;

import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Repository("redisDao")
public class RedisDaoImpl implements RedisDao {
    @Resource
    RedisTemplate<String, Object> redisTemplate;
    @Resource
    ValueOperations<String, Object> valueOperations;
    @Resource
    ListOperations<String, Object> listOperations;
    @Resource
    HashOperations<String, String, Object> hashOperations;
    @Resource
    SetOperations<String, Object> setOperations;
    @Resource
    ZSetOperations<String, Object> zSetOperations;

    /**
     * ========================= key的操作==============
     */
    /**
     * 判断key是否存在
     *
     * @param key
     * @return
     */
    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 设置过期时间
     *
     * @param key
     */
    public Boolean expire(String key, long timeout, final TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }

    public Boolean expire(String key, Date date) {
        return redisTemplate.expireAt(key, date);
    }

    public Boolean del(String key) {
        return redisTemplate.delete(key);
    }

    /**
     *
     * @param key
     * @return
     */
    public Long delKeys(String... key) {
        return redisTemplate.delete(Arrays.asList(key));
    }

    /**
     * 删除多个key
     *
     * @param keys
     * @return
     */
    public Long delKeys(Collection<String> keys) {
        return redisTemplate.delete(keys);
    }

}
